package com.lzs.esubscribe.config;

/**
 * @author :liangzesheng
 * @Date :2022-02-15
 * @Description : 跨域请求处理类 解决跨域问题
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {
    private CorsConfiguration buildConfig(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");//允许所有的源
        corsConfiguration.addAllowedHeader("*");//允许所有的标头列表
        corsConfiguration.addAllowedMethod("*");//允许所有的定义方法，未定义则使用有RuqestMap注释定义的方法
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter(){
        //注册配置
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",buildConfig());
        return new CorsFilter(source);
    }
}
